<?php

//-------------------------------------------------------------------
//Nazov:		Invoice_Insert
//Popis:		Vytvorenie faktury na zaklade db tabuliek objednavok
//Poznamka:		
//Navrat.hodn:	Typ bool - uspesnost zapisu do DB
//-------------------------------------------------------------------
function Invoice_Insert($Order_Head_ID){
	do{
                //udaje z invoice_options 	
		$sql_obj = mysql_fetch_object((mysql_query("select * from invoice_options")));
                
                //test, ci je vytvaranie faktur povolene
                if(trim($sql_obj->Active)!="1") break;
        
                //objekt pre pristup k niektorym datam z objednavky
                $_OrderService = new classOrderService();
		//test, ci uz nie je faktura vytvorena
		$sql_obj = mysql_fetch_row((mysql_query("select COUNT(*) from invoice_heads where IH_Order=$Order_Head_ID")));
		if($sql_obj[0] != 0) break;
		//vytvorenie poradoveho cisla faktury
		$sql_obj = mysql_fetch_array((mysql_query("select MAX(IH_No) from invoice_heads")));	
		$IH_No=$sql_obj[0]+1;
		//nastavenie dat na hlavicku
	  	$sql="SELECT * FROM order_heads WHERE ID=".$Order_Head_ID;
		$sql_array = mysql_fetch_object(mysql_query($sql));
		
		//udaje z invoice_options 	
		$sql_obj = mysql_fetch_object((mysql_query("select * from invoice_options")));		
		$Prefix			= $sql_obj->Prefix;
		$Constant_Symbol	= $sql_obj->Constant_Symbol;
                if(trim($sql_obj->Payment_Type)!="") 
                   $Payment_Type        = $sql_obj->Payment_Type;
                else 
                   $Payment_Type        = $_OrderService->getPaymentName($sql_array->OH_Payment);
		$Additorial_Text	= $sql_obj->Additorial_Text;
		
		$order_date = getdate(strtotime($sql_array->Order_Create)); 
		$mesiac = $order_date['mon']; 
		$mday = $order_date['mday']; 
		$rok = $order_date['year'];
		$Expiration = date('Y-m-d',mktime(0,0,0,$mesiac,($mday + $sql_obj->Expiration),$rok));
		
		//vytvorenie sql pre ulozenie hlavicky
		$sql1 = "INSERT INTO invoice_heads (IH_Order,IH_No,IH_Create,IH_Expiration,IH_Tax,IH_Prefix,
                        IH_Constant_Symbol,IH_Additorial_Text,IH_Payment_Type,IH_UserID,IH_Organisation,IH_Name,
                        IH_Surname,IH_City,IH_Street,IH_PostID,IH_Phone,IH_Mobile,IH_Email,IH_ICO,IH_DIC,IH_Delivery) 
                        VALUES ("
                                ."'".$sql_array->ID		."'".","
                                ."'".$IH_No			."'".","
                                ."'".$sql_array->Order_Create	."'".","
                                ."'".$Expiration		."'".","	            
                                ."'".$sql_array->Order_Create	."'".","
                                ."'".$Prefix			."'".","
                                ."'".$Constant_Symbol		."'".","
                                ."'".$Additorial_Text		."'".","
                                ."'".$Payment_Type		."'".","
                                ."'".$sql_array->OH_UserID	."'".","
                                ."'".$sql_array->OH_Organisation."'".","
                                ."'".$sql_array->OH_Name	."'".","
                                ."'".$sql_array->OH_Surname	."'".","
                                ."'".$sql_array->OH_City	."'".","
                                ."'".$sql_array->OH_Street	."'".","
                                ."'".$sql_array->OH_PostID	."'".","
                                ."'".$sql_array->OH_Phone	."'".","
                                ."'".$sql_array->OH_Mobile	."'".","
                                ."'".$sql_array->OH_Email	."'".","
                                ."'".$sql_array->OH_ICO		."'".","
                                ."'".$sql_array->OH_DIC		."'".","
                                ."'".$_OrderService->getDeliveryName($sql_array->OH_Delivery)."'".");";
                                //print $sql1;
		//----------ulozenie hlavicky objednavky + zistenie priradeneho ID-------
		mysql_query($sql1);
		$IH_ID = mysql_insert_id();
		if ($IH_ID==0) {
		  print INV_CHYBA_PRI_UKLADANI;
		  break;
		}
		//-----------------------------------------------------------------------
		
		//sql na vyfiltrovanie jednotlivych produktov            
		$sql="SELECT * FROM order_products WHERE OH_ID=".$Order_Head_ID;
		$sql1="";
		$query = mysql_query($sql);
		while($sql_array = mysql_fetch_object($query)){
                    //vytvorenie sql pre ulozenie tovarov v objednavke
                    $sql1=" INSERT INTO invoice_products (IH_ID,IP_Code,IP_Name,IP_Price,IP_DPH,IP_Count,IP_UnitType,IP_ID) 
                            VALUES ("
                            ."".$IH_ID			."".","
                            ."'".$sql_array->OP_Code	."'".","
                            ."'".$sql_array->OP_Name	."'".","
                            ."".$sql_array->OP_Price	."".","
                            ."".$sql_array->OP_DPH		."".","
                            ."".$sql_array->OP_Count	."".","
                            ."'".$sql_array->OP_UnitType    ."'".","
                            ."".$sql_array->OP_ID		.""."); ";
                    //@$sql_result = $sql_result and 
                    mysql_query($sql1);       
		}
		
		print "
		<script language=\"javascript\">
		<!--
		        window.alert('".INV_POTVRDENIM_OBJEDNAVKY_BOLA_VYTVORENA_FAKTURA."');
		-->
		</script>
		";	
	
		return $sql_result;
	}while(false);
}

//-------------------------------------------------------------------
//Nazov:		Invoice_Delete
//Popis:		Odstranenie faktury z DB
//Poznamka:		
//Navrat.hodn:	Typ bool - uspesnost zapisu do DB
//-------------------------------------------------------------------
function Invoice_Delete($ID){
    if(($ID!="") AND ($ID!=0)){
        $sql="DELETE FROM invoice_heads WHERE ID=".$ID;
		mysql_query($sql);
		$sql="DELETE FROM invoice_products WHERE IH_ID=".$ID;
		mysql_query($sql);
        return true;
    }
    return false;
}


//-------------------------------------------------------------------
//Nazov:		Invoice_Archive_Insert
//Popis:		Ulozenie objednavky z tabuliek objednavok do tabuliek archivu objednavok
//Poznamka:		Funkcia je volana z miesta tesne pred vymazanim faktury v subore order_management.php
//Navrat.hodn:	Typ bool - uspesnost zapisu do DB
//-------------------------------------------------------------------
function Invoice_Archive_Insert($Invoice_Head_ID){
	//nastavenie dat na hlavicku
  	$sql="SELECT * FROM invoice_heads WHERE ID=".$Invoice_Head_ID;
	$sql_array = mysql_fetch_object(mysql_query($sql));
	//vytvorenie sql pre ulozenie hlavicky
	$sql1 = "INSERT INTO invoice_heads_archive (IHA_No,IHA_Create,IHA_Expiration,IHA_Tax,IHA_Prefix,IHA_Constant_Symbol,IHA_Additorial_Text,
			IHA_Payment_Type,IHA_UserID,IHA_Organisation,IHA_Name,IHA_Surname,IHA_City,IHA_Street,IHA_PostID,IHA_Phone,IHA_Mobile,
			IHA_Email,IHA_ICO,IHA_DIC,IHA_Delivery) VALUES ("
				."'".$sql_array->IH_No	."'".","
	            ."'".$sql_array->IH_Create		."'".","
	            ."'".$sql_array->IH_Expiration	."'".","
	            ."'".$sql_array->IH_Tax		."'".","
	            ."'".$sql_array->IH_Prefix		."'".","
	            ."'".$sql_array->IH_Constant_Symbol	."'".","
	            ."'".$sql_array->IH_Additorial_Text	."'".","
	            ."'".$sql_array->IH_Payment_Type	."'".","
	            ."'".$sql_array->IH_UserID		."'".","
	            ."'".$sql_array->IH_Organisation	."'".","
	            ."'".$sql_array->IH_Name		."'".","
	            ."'".$sql_array->IH_Surname		."'".","
	            ."'".$sql_array->IH_City		."'".","
	            ."'".$sql_array->IH_Street		."'".","
	            ."'".$sql_array->IH_PostID		."'".","
	            ."'".$sql_array->IH_Phone		."'".","
	            ."'".$sql_array->IH_Mobile		."'".","
	            ."'".$sql_array->IH_Email		."'".","
	            ."'".$sql_array->IH_ICO		."'".","
	            ."'".$sql_array->IH_DIC		."'".","
                    ."'".$sql_array->IH_Delivery	."'".");";
				
				
				//print $sql1;
	            
	//----------ulozenie hlavicky objednavky + zistenie priradeneho ID-------
	mysql_query($sql1);
	$IHA_ID = mysql_insert_id();
	if ($IHA_ID==0) {
	  print INV_CHYBA_PRI_UKLADANI_FAKTURY_DO_ARCHIVU;
	  break;
	}
	//-----------------------------------------------------------------------
	
	//sql na vyfiltrovanie jednotlivych produktov            
	$sql="SELECT * FROM invoice_products WHERE IH_ID=".$Invoice_Head_ID;
	$sql1="";
	$query = mysql_query($sql);
	while($sql_array = mysql_fetch_object($query)){
	  	//vytvorenie sql pre ulozenie tovarov v objednavke
	  	//euro:
		if($sql_array->IP_Euro=="") $IP_Euro=0; else $IP_Euro=$sql_array->IP_Euro;
		
	  	$sql1="INSERT INTO invoice_products_archive (IHA_ID,IPA_Code,IPA_Name,IPA_Price,IPA_DPH,IPA_Count,IPA_UnitType,IPA_ID) VALUES ("
	  	    ."". $IHA_ID		."".","
	            ."'".$sql_array->IP_Code	."'".","
	            ."'".$sql_array->IP_Name	."'".","
	            ."". $sql_array->IP_Price	."".","
                    ."". $sql_array->IP_DPH	."".","
	            ."". $sql_array->IP_Count	."".","
	            ."'".$sql_array->IP_UnitType."'".","
	            ."". $sql_array->IP_ID	.""."); ";
		mysql_query($sql1);       
	}	
	return $sql_result;
}

//-------------------------------------------------------------------
//Nazov:		Invoice_Year_Switch
//Popis:		Presunutie vsetkych faktur do archivu a vymazanie povodnych z DB tabuliek 
//Poznamka:		
//Navrat.hodn:	Typ bool - uspesnost zapisu do DB
//-------------------------------------------------------------------
function Invoice_Year_Switch($YearFrom,$YearTo){
	do{
		//test spravnosti zadania rokov - duplicitna polozka zaradena z dovodu omylnosti cloveka :-)
		if (!((is_numeric($YearFrom)) and (is_numeric($YearTo)) and ($YearFrom<$YearTo))) return false;
		//presun dat
		$query = mysql_query("SELECT ID FROM invoice_heads WHERE YEAR(IH_Create)=".$YearFrom);
		while($sql_array = mysql_fetch_object($query)){
			Invoice_Archive_Insert($sql_array->ID);
		}
		//vymazanie dat
		$query = mysql_query("SELECT ID FROM invoice_heads WHERE YEAR(IH_Create)=".$YearFrom);
		while($sql_array = mysql_fetch_object($query)){
			mysql_query("DELETE FROM invoice_products WHERE IH_ID=".$sql_array->ID);
		}
		mysql_query("DELETE FROM invoice_heads WHERE YEAR(IH_Create)=$YearFrom");
		return true;
	}while(false);	
}


//-------------------------------------------------------------------
//Nazov:		Invoice_Year_Switch
//Popis:		funkcia ktora zobrazi vsetky typy doruceni v textboxoch a zarovaňen aj formular pre pridanie
//Poznamka:		
//Navrat.hodn:	Typ bool - uspesnost zapisu do DB
//-------------------------------------------------------------------
function DeliveryTypeForm(){
	do{
		//test spravnosti zadania rokov - duplicitna polozka zaradena z dovodu omylnosti cloveka :-)
		if (!((is_numeric($YearFrom)) and (is_numeric($YearTo)) and ($YearFrom<$YearTo))) return false;
		//presun dat
		$query = mysql_query("SELECT ID FROM invoice_heads WHERE YEAR(IH_Create)=".$YearFrom);
		while($sql_array = mysql_fetch_object($query)){
			Invoice_Archive_Insert($sql_array->ID);
		}
		//vymazanie dat
		$query = mysql_query("SELECT ID FROM invoice_heads WHERE YEAR(IH_Create)=".$YearFrom);
		while($sql_array = mysql_fetch_object($query)){
			mysql_query("DELETE FROM invoice_products WHERE IH_ID=".$sql_array->ID);
		}
		mysql_query("DELETE FROM invoice_heads WHERE YEAR(IH_Create)=$YearFrom");
		return true;
	}while(false);	
}


?>